package ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import model.Country;
import model.Customer;
import model.Outsider;
import model.Project;
import model.Statistics;
import controller.StatisticsController;
import util.DoubleComparator;
import util.ImagePanel;
import util.IntComparator;
import util.Menu;
import util.PdfFilePrint;

public class StatisticsUI implements ActionListener
{
	private static StatisticsUI instance = null;

	private StatisticsController statsCtrl;

	private JMenuBar menu;

	private Menu menuBar;

	private JFrame frameStatisticsUI;

	private JTabbedPane tabbedPane;

	private JPanel redPanel;

	private Image image = null;

	private ImagePanel statsProjectCustomer;
	private ImagePanel statsProjectRevenue;
	private ImagePanel statsSupplierName;
	private ImagePanel statsSupplierCountry;

	private DefaultTableModel statsProjectCustomerTableModel;
	private DefaultTableModel statsProjectRevenueTableModel;
	private DefaultTableModel statsSupplierNameTableModel;
	private DefaultTableModel statsSupplierCountryTableModel;

	private JTable statsProjectCustomerTable;
	private JTable statsProjectRevenueTable;
	private JTable statsSupplierNameTable;
	private JTable statsSupplierCountryTable;

	private JCheckBox statsProjectCustomerCheckBoxLocation;
	private JCheckBox statsProjectRevenueCheckBoxLocation;

	private JComboBox<String> statsProjectCustomerComboBoxCountry;
	private JComboBox<String> statsProjectCustomerComboBoxYear;
	private JComboBox<String> statsProjectCustomerComboBoxCity;
	private JComboBox<String> statsProjectCustomerComboBoxMonth;
	private JComboBox<String> statsProjectRevenueComboBoxMonth;
	private JComboBox<String> statsProjectRevenueComboBoxYear;
	private JComboBox<String> statsProjectRevenueComboBoxCountry;
	private JComboBox<String> statsProjectRevenueComboBoxCity;
	private JComboBox<String> statsSupplierNameComboBoxCity;
	private JComboBox<String> statsSupplierNameComboBoxCountry;

	private JButton statsProjectCustomerButtonSearch;
	private JButton statsProjectRevenueButtonSearch;
	private JButton statsSupplierCountryButtonRefresh;

	private JLabel statsProjectCustomerLabelResults;

	private JTextField statsProjectCustomerTextFieldFrom;
	private JTextField statsProjectCustomerTextFieldTo;
	private JTextField statsProjectRevenueTextFieldFrom;
	private JTextField statsProjectRevenueTextFieldTo;

	private ButtonGroup statsProjectCustomerButtonGroup;
	private ButtonGroup statsProjectRevenueButtonGroup;

	private JRadioButton statsProjectCustomerRadioButtonPeriod;
	private JRadioButton statsProjectCustomerRadioButtonNone;
	private JRadioButton statsProjectCustomerRadioButtonAnually;
	private JRadioButton statsProjectRevenueRadioButtonAnually;
	private JRadioButton statsProjectRevenueRadioButtonNone;
	private JRadioButton statsProjectRevenueRadioButtonPeriod;

	private JLabel statsProjectRevenueLabelProjects;

	private backButton back;
	private JButton btnBack;
	private JButton btnPrint;

	public void display()
	{
		frameStatisticsUI.getContentPane().add(redPanel);

		back = new backButton();
		back.setNumber("13");

		redPanel.add(tabbedPane);

		tabbedPane.add("Customer Project", statsProjectCustomer);
		tabbedPane.add("Project Revenue", statsProjectRevenue);
		tabbedPane.add("Supplier Name", statsSupplierName);
		tabbedPane.add("Supplier Country", statsSupplierCountry);

		frameStatisticsUI.setSize(800,420);
		frameStatisticsUI.setLocationRelativeTo(null);
		frameStatisticsUI.setVisible(true);
		frameStatisticsUI.setResizable(false);
	}

	public StatisticsUI()
	{
		frameStatisticsUI = new JFrame("Customer UI");
		frameStatisticsUI.setTitle("Statistics UI");

		menuBar = Menu.getInstance();

		menu = menuBar.makeJMenuBar();

		frameStatisticsUI.setJMenuBar(menu);

		redPanel = new JPanel();
		redPanel.setBackground(new Color(165, 42, 42));

		tabbedPane = new JTabbedPane();

		statsCtrl = new StatisticsController();

		try
		{
			image = ImageIO.read(new File("img/background/background.jpg"));
		} 
		catch (IOException e)
		{
			msgError(null, "Could not load background image");
		}

		JLabel redPanelLabelTitle = new JLabel("STATISTICS MENU");
		redPanelLabelTitle.setForeground(Color.WHITE);
		redPanelLabelTitle.setFont(new Font("Arial Unicode MS", Font.BOLD, 20));

		btnBack = new JButton("Back");
		btnBack.addActionListener(this);
		btnBack.setOpaque(false);
		btnBack.setBounds(9, 448, 100, 20);

		btnPrint = new JButton("Print");
		btnPrint.addActionListener(this);
		btnPrint.setOpaque(false);

		GroupLayout gl_redTitle = new GroupLayout(redPanel);
		gl_redTitle.setHorizontalGroup(
				gl_redTitle.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_redTitle.createSequentialGroup()
						.addGroup(gl_redTitle.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_redTitle.createSequentialGroup()
										.addContainerGap()
										.addComponent(redPanelLabelTitle, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE))
										.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 795, GroupLayout.PREFERRED_SIZE)
										.addGroup(gl_redTitle.createSequentialGroup()
												.addContainerGap()
												.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.RELATED, 605, Short.MAX_VALUE)
												.addComponent(btnPrint, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)))
												.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		gl_redTitle.setVerticalGroup(
				gl_redTitle.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_redTitle.createSequentialGroup()
						.addComponent(redPanelLabelTitle)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 309, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_redTitle.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnPrint, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
								.addContainerGap(25, Short.MAX_VALUE))
				);

		redPanel.setLayout(gl_redTitle);

		/******************************************************
		 * 
		 * statsProjectCustomer pane
		 * 
		 ******************************************************/

		statsProjectCustomer = new ImagePanel(image);

		String[] statsProjectCustomerTableColumns = { "#", "Customer", "City", "Country", "", "Projects", "Project Price", "Item Price", "Revenue" };

		statsProjectCustomerTableModel = new DefaultTableModel(null, statsProjectCustomerTableColumns);
		statsProjectCustomerTable = new JTable(statsProjectCustomerTableModel)
		{
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) 
			{
				return false;
			}

			public Class<?> getColumnClass(int column)   
			{   
				return getValueAt(0, column).getClass();   
			} 
		};

		Dimension dim = statsProjectCustomerTable.getPreferredSize(); 
		double width = dim.width / statsProjectCustomerTableColumns.length; 
		double[] width_2 = { 0.3, 2, 0.8, 0.8, 0.3, 0.7, 1.2, 1.2, 1.2 };

		for (int i = 0; i < statsProjectCustomerTableColumns.length; i++)
			statsProjectCustomerTable.getColumnModel().getColumn(i).setPreferredWidth((int) (width*width_2[i])); 

		TableRowSorter<DefaultTableModel> statsProjectCustomerTableSorter = new TableRowSorter<DefaultTableModel>(statsProjectCustomerTableModel);

		statsProjectCustomerTableSorter.setComparator(0, new IntComparator());
		statsProjectCustomerTableSorter.setComparator(5, new IntComparator());
		statsProjectCustomerTableSorter.setComparator(6, new DoubleComparator());
		statsProjectCustomerTableSorter.setComparator(7, new DoubleComparator());
		statsProjectCustomerTableSorter.setComparator(8, new DoubleComparator());

		statsProjectCustomerTable.setRowSorter(statsProjectCustomerTableSorter);

		statsProjectCustomerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane statsProjectCustomerTableSrollPane = new JScrollPane(statsProjectCustomerTable);

		JPanel statsProjectCustomerPanelPeriod = new JPanel();
		statsProjectCustomerPanelPeriod.setOpaque(false);
		statsProjectCustomerPanelPeriod.setBorder(new TitledBorder(null, "Period filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel statsProjectCustomerPanelLocation = new JPanel();
		statsProjectCustomerPanelLocation.setOpaque(false);
		statsProjectCustomerPanelLocation.setBorder(new TitledBorder(null, "Location filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel statsProjectCustomerPanelSearch = new JPanel();
		statsProjectCustomerPanelSearch.setOpaque(false);
		statsProjectCustomerPanelSearch.setBorder(new TitledBorder(null, "Search", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		GroupLayout gl_newCustomer = new GroupLayout(statsProjectCustomer);
		gl_newCustomer.setHorizontalGroup(
				gl_newCustomer.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newCustomer.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_newCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(statsProjectCustomerTableSrollPane, GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE)
								.addGroup(gl_newCustomer.createSequentialGroup()
										.addComponent(statsProjectCustomerPanelPeriod, GroupLayout.PREFERRED_SIZE, 370, GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(statsProjectCustomerPanelLocation, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(statsProjectCustomerPanelSearch, GroupLayout.PREFERRED_SIZE, 133, Short.MAX_VALUE)))
										.addContainerGap())
				);
		gl_newCustomer.setVerticalGroup(
				gl_newCustomer.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newCustomer.createSequentialGroup()
						.addGap(12)
						.addGroup(gl_newCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(statsProjectCustomerPanelSearch, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(gl_newCustomer.createParallelGroup(Alignment.LEADING, false)
										.addComponent(statsProjectCustomerPanelPeriod, GroupLayout.PREFERRED_SIZE, 90, Short.MAX_VALUE)
										.addComponent(statsProjectCustomerPanelLocation, GroupLayout.PREFERRED_SIZE, 90, Short.MAX_VALUE)))
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(statsProjectCustomerTableSrollPane, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
										.addContainerGap())
				);
		JLabel statsProjectCustomerLabelResultsFound = new JLabel("Customers found:");
		JLabel statsProjectCustomerLabelCountry = new JLabel("Country:");
		statsProjectCustomerLabelCountry.setHorizontalTextPosition(SwingConstants.RIGHT);
		statsProjectCustomerLabelCountry.setHorizontalAlignment(SwingConstants.RIGHT);
		JLabel statsProjectCustomerLabelCity = new JLabel("City:");
		statsProjectCustomerLabelCity.setHorizontalAlignment(SwingConstants.RIGHT);
		statsProjectCustomerLabelCity.setHorizontalTextPosition(SwingConstants.RIGHT);
		JLabel statsProjectCustomerLabelEmpty = new JLabel("");
		statsProjectCustomerLabelResults = new JLabel("0");

		statsProjectCustomerButtonSearch = new JButton("Search");
		statsProjectCustomerButtonSearch.addActionListener(this);
		statsProjectCustomerButtonSearch.setOpaque(false);

		statsProjectCustomerPanelSearch.add(statsProjectCustomerLabelResultsFound);
		statsProjectCustomerPanelSearch.add(statsProjectCustomerLabelResults);
		statsProjectCustomerPanelSearch.add(statsProjectCustomerButtonSearch);

		statsProjectCustomerCheckBoxLocation = new JCheckBox("Include location");
		statsProjectCustomerCheckBoxLocation.setActionCommand("Include location");
		statsProjectCustomerCheckBoxLocation.setOpaque(false);

		statsProjectCustomerComboBoxCountry = new JComboBox<String>();
		statsProjectCustomerComboBoxCountry.addActionListener(this);
		statsProjectCustomerComboBoxCountry.setEnabled(false);
		statsProjectCustomerComboBoxCity = new JComboBox<String>();
		statsProjectCustomerComboBoxCity.setEnabled(false);
		statsProjectCustomerCheckBoxLocation.addActionListener(this);

		statsProjectCustomerPanelLocation.setLayout(new GridLayout(0, 2, 5, 5));
		statsProjectCustomerPanelLocation.add(statsProjectCustomerLabelEmpty);
		statsProjectCustomerPanelLocation.add(statsProjectCustomerCheckBoxLocation);
		statsProjectCustomerPanelLocation.add(statsProjectCustomerLabelCountry);
		statsProjectCustomerPanelLocation.add(statsProjectCustomerComboBoxCountry);
		statsProjectCustomerPanelLocation.add(statsProjectCustomerLabelCity);
		statsProjectCustomerPanelLocation.add(statsProjectCustomerComboBoxCity);

		statsProjectCustomerRadioButtonPeriod = new JRadioButton("Period");
		statsProjectCustomerRadioButtonPeriod.setOpaque(false);
		statsProjectCustomerRadioButtonPeriod.addActionListener(this);

		statsProjectCustomerRadioButtonAnually = new JRadioButton("Anually");
		statsProjectCustomerRadioButtonAnually.setOpaque(false);
		statsProjectCustomerRadioButtonAnually.addActionListener(this);

		statsProjectCustomerRadioButtonNone = new JRadioButton("None", true);
		statsProjectCustomerRadioButtonNone.setOpaque(false);
		statsProjectCustomerRadioButtonNone.addActionListener(this);

		statsProjectCustomerButtonGroup = new ButtonGroup();
		statsProjectCustomerButtonGroup.add(statsProjectCustomerRadioButtonNone);
		statsProjectCustomerButtonGroup.add(statsProjectCustomerRadioButtonAnually);
		statsProjectCustomerButtonGroup.add(statsProjectCustomerRadioButtonPeriod);	

		JLabel statsProjectCustomerLabelFrom = new JLabel("From:");
		statsProjectCustomerLabelFrom.setHorizontalTextPosition(SwingConstants.RIGHT);
		statsProjectCustomerLabelFrom.setHorizontalAlignment(SwingConstants.RIGHT);

		JLabel statsProjectCustomerLabelTo = new JLabel("To:");
		statsProjectCustomerLabelTo.setHorizontalAlignment(SwingConstants.RIGHT);
		statsProjectCustomerLabelTo.setHorizontalTextPosition(SwingConstants.RIGHT);

		JLabel statsProjectCustomerLabelYear = new JLabel("Year:");
		statsProjectCustomerLabelYear.setHorizontalAlignment(SwingConstants.RIGHT);
		statsProjectCustomerLabelYear.setHorizontalTextPosition(SwingConstants.RIGHT);

		JLabel statsProjectCustomerLabelMonth = new JLabel("Month:");
		statsProjectCustomerLabelMonth.setHorizontalAlignment(SwingConstants.RIGHT);
		statsProjectCustomerLabelMonth.setHorizontalTextPosition(SwingConstants.RIGHT);

		statsProjectCustomerTextFieldFrom = new JTextField();
		statsProjectCustomerTextFieldFrom.setEnabled(false);

		statsProjectCustomerTextFieldTo = new JTextField();
		statsProjectCustomerTextFieldTo.setEnabled(false);

		statsProjectCustomerComboBoxYear = new JComboBox<String>();
		statsProjectCustomerComboBoxYear.addActionListener(this);
		statsProjectCustomerComboBoxYear.setEnabled(false);

		statsProjectCustomerComboBoxMonth = new JComboBox<String>();
		statsProjectCustomerComboBoxMonth.setEnabled(false);

		statsProjectCustomerPanelPeriod.setLayout(new GridLayout(0, 5, 2, 2));
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerRadioButtonPeriod);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerLabelFrom);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerTextFieldFrom);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerLabelTo);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerTextFieldTo);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerRadioButtonAnually);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerLabelYear);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerComboBoxYear);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerLabelMonth);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerComboBoxMonth);
		statsProjectCustomerPanelPeriod.add(statsProjectCustomerRadioButtonNone);

		statsProjectCustomer.setLayout(gl_newCustomer);

		/******************************************************
		 * 
		 * statsSupplierName pane
		 * 
		 ******************************************************/

		statsSupplierName = new ImagePanel(image);

		String[] statsSupplierNameTableColumns = { "#", "Supplier Name", "Number of Items", "Country", "City", "", "Active" };

		statsSupplierNameTableModel = new DefaultTableModel(null, statsSupplierNameTableColumns);
		statsSupplierNameTable = new JTable(statsSupplierNameTableModel)
		{
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) 
			{
				return false;
			}

			public Class<?> getColumnClass(int column)   
			{   
				return getValueAt(0, column).getClass();   
			} 
		};

		dim = statsSupplierNameTable.getPreferredSize(); 
		width = dim.width / statsSupplierNameTableColumns.length; 
		double[] width_3 = { 0.3, 2, 1, 1.2, 1, 0.3, 0.3 };

		for (int i = 0; i < statsSupplierNameTableColumns.length; i++)
			statsSupplierNameTable.getColumnModel().getColumn(i).setPreferredWidth((int) (width*width_3[i])); 

		TableRowSorter<DefaultTableModel> statsSupplierNameTableSorter = new TableRowSorter<DefaultTableModel>(statsSupplierNameTableModel);

		statsSupplierNameTableSorter.setComparator(0, new IntComparator());
		statsSupplierNameTableSorter.setComparator(2, new IntComparator());

		statsSupplierNameTable.setRowSorter(statsSupplierNameTableSorter);

		statsSupplierNameTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane statsSupplierNameTableScrollPane = new JScrollPane(statsSupplierNameTable);

		JPanel statsSupplierNamePanelLocation = new JPanel();
		statsSupplierNamePanelLocation.setOpaque(false);
		statsSupplierNamePanelLocation.setBorder(new TitledBorder(null, "Location filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		GroupLayout gl_statsSupplierName = new GroupLayout(statsSupplierName);
		gl_statsSupplierName.setHorizontalGroup(
				gl_statsSupplierName.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_statsSupplierName.createSequentialGroup()
						.addContainerGap(87, Short.MAX_VALUE)
						.addGroup(gl_statsSupplierName.createParallelGroup(Alignment.LEADING)
								.addComponent(statsSupplierNameTableScrollPane, GroupLayout.PREFERRED_SIZE, 603, GroupLayout.PREFERRED_SIZE)
								.addComponent(statsSupplierNamePanelLocation, GroupLayout.PREFERRED_SIZE, 434, GroupLayout.PREFERRED_SIZE))
								.addGap(100))
				);
		gl_statsSupplierName.setVerticalGroup(
				gl_statsSupplierName.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_statsSupplierName.createSequentialGroup()
						.addContainerGap()
						.addComponent(statsSupplierNamePanelLocation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(statsSupplierNameTableScrollPane, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
						.addContainerGap())
				);
		statsSupplierNamePanelLocation.setLayout(new GridLayout(1, 0, 5, 5));

		JLabel statsSupplierNameLabelCountry = new JLabel("Country:");
		statsSupplierNameLabelCountry.setHorizontalAlignment(SwingConstants.RIGHT);

		JLabel statsSupplierNameLabelCity = new JLabel("City:");
		statsSupplierNameLabelCity.setHorizontalAlignment(SwingConstants.RIGHT);

		statsSupplierNameComboBoxCountry = new JComboBox<String>();
		statsSupplierNameComboBoxCountry.addActionListener(this);

		statsSupplierNameComboBoxCity = new JComboBox<String>();
		statsSupplierNameComboBoxCity.addActionListener(this);

		statsSupplierNamePanelLocation.add(statsSupplierNameLabelCountry);		
		statsSupplierNamePanelLocation.add(statsSupplierNameComboBoxCountry);
		statsSupplierNamePanelLocation.add(statsSupplierNameLabelCity);
		statsSupplierNamePanelLocation.add(statsSupplierNameComboBoxCity);
		statsSupplierName.setLayout(gl_statsSupplierName);

		/******************************************************
		 * 
		 * statsProjectRevenue pane
		 * 
		 ******************************************************/

		statsProjectRevenue = new ImagePanel(image);

		String[] statsProjectRevenueTableColumns = { "#", "Project Name", "Price Item", "Price Project", "Revenue", "Sub Contractor Money", "Customer", "City", "Country", "", "Date" };

		statsProjectRevenueTableModel = new DefaultTableModel(null, statsProjectRevenueTableColumns);
		statsProjectRevenueTable = new JTable(statsProjectRevenueTableModel)
		{

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) 
			{
				return false;
			}

			public Class<?> getColumnClass(int column)   
			{   
				return getValueAt(0, column).getClass();   
			} 
		};

		dim = statsProjectRevenueTable.getPreferredSize(); 
		width = dim.width / statsProjectRevenueTableColumns.length; 
		double[] width2 = { 0.3, 1.5, 1, 1.1, 1.1, 1.1, 1.1, 0.8, 0.8, 0.2, 0.8 };

		for (int i = 0; i < statsProjectRevenueTableColumns.length; i++)
			statsProjectRevenueTable.getColumnModel().getColumn(i).setPreferredWidth((int) (width*width2[i])); 

		TableRowSorter<DefaultTableModel> statsProjectRevenueTableSorter = new TableRowSorter<DefaultTableModel>(statsProjectRevenueTableModel);

		statsProjectRevenueTableSorter.setComparator(0, new IntComparator());
		statsProjectRevenueTableSorter.setComparator(2, new DoubleComparator());
		statsProjectRevenueTableSorter.setComparator(3, new DoubleComparator());
		statsProjectRevenueTableSorter.setComparator(4, new DoubleComparator());
		statsProjectRevenueTableSorter.setComparator(5, new DoubleComparator());

		statsProjectRevenueTable.setRowSorter(statsProjectRevenueTableSorter);

		statsProjectRevenueTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane statsProjectRevenueTableSrollPane = new JScrollPane(statsProjectRevenueTable);

		JPanel statsProjectRevenuePanelPeriod = new JPanel();
		statsProjectRevenuePanelPeriod.setOpaque(false);
		statsProjectRevenuePanelPeriod.setBorder(new TitledBorder(null, "Period filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel statsProjectRevenuePanelLocation = new JPanel();
		statsProjectRevenuePanelLocation.setOpaque(false);
		statsProjectRevenuePanelLocation.setBorder(new TitledBorder(null, "Location filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel statsProjectRevenuePanelSearch = new JPanel();
		statsProjectRevenuePanelSearch.setOpaque(false);
		statsProjectRevenuePanelSearch.setBorder(new TitledBorder(null, "Search", TitledBorder.LEADING, TitledBorder.TOP, null, null));


		GroupLayout gl_statsProjectRevenue = new GroupLayout(statsProjectRevenue);
		gl_statsProjectRevenue.setHorizontalGroup(
				gl_statsProjectRevenue.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_statsProjectRevenue.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_statsProjectRevenue.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_statsProjectRevenue.createSequentialGroup()
										.addComponent(statsProjectRevenuePanelPeriod, GroupLayout.PREFERRED_SIZE, 377, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(statsProjectRevenuePanelLocation, GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(statsProjectRevenuePanelSearch, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
										.addComponent(statsProjectRevenueTableSrollPane, GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE))
										.addContainerGap())
				);
		gl_statsProjectRevenue.setVerticalGroup(
				gl_statsProjectRevenue.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_statsProjectRevenue.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_statsProjectRevenue.createParallelGroup(Alignment.BASELINE)
								.addComponent(statsProjectRevenuePanelPeriod, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
								.addComponent(statsProjectRevenuePanelLocation, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
								.addComponent(statsProjectRevenuePanelSearch, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(statsProjectRevenueTableSrollPane, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
				);

		JLabel statsProjectRevenueLabelProjectsFound = new JLabel("Projects found:");
		statsProjectRevenueLabelProjects = new JLabel("0");

		statsProjectRevenueButtonSearch = new JButton("Search");
		statsProjectRevenueButtonSearch.setOpaque(false);
		statsProjectRevenueButtonSearch.addActionListener(this);

		statsProjectRevenuePanelSearch.add(statsProjectRevenueLabelProjectsFound);
		statsProjectRevenuePanelSearch.add(statsProjectRevenueLabelProjects);
		statsProjectRevenuePanelSearch.add(statsProjectRevenueButtonSearch);

		/* LOCATION */

		JLabel statsProjectRevenueLabelEmpty = new JLabel("");
		JLabel statsProjectRevenueLabelCountry = new JLabel("Country:");
		JLabel statsProjectRevenueLabelCity = new JLabel("City:");

		statsProjectRevenueLabelCountry.setHorizontalAlignment(SwingConstants.RIGHT);
		statsProjectRevenueLabelCity.setHorizontalAlignment(SwingConstants.RIGHT);

		statsProjectRevenueCheckBoxLocation = new JCheckBox("Include location");
		statsProjectRevenueCheckBoxLocation.setOpaque(false);
		statsProjectRevenueCheckBoxLocation.addActionListener(this);

		statsProjectRevenueComboBoxCountry = new JComboBox<String>();		
		statsProjectRevenueComboBoxCountry.setEnabled(false);
		statsProjectRevenueComboBoxCountry.addActionListener(this);

		statsProjectRevenueComboBoxCity = new JComboBox<String>();
		statsProjectRevenueComboBoxCity.setEnabled(false);

		statsProjectRevenuePanelLocation.setLayout(new GridLayout(0, 2, 5, 5));
		statsProjectRevenuePanelLocation.add(statsProjectRevenueLabelEmpty);
		statsProjectRevenuePanelLocation.add(statsProjectRevenueCheckBoxLocation);
		statsProjectRevenuePanelLocation.add(statsProjectRevenueLabelCountry);
		statsProjectRevenuePanelLocation.add(statsProjectRevenueComboBoxCountry);
		statsProjectRevenuePanelLocation.add(statsProjectRevenueLabelCity);
		statsProjectRevenuePanelLocation.add(statsProjectRevenueComboBoxCity);

		/* PERIOD */
		JLabel statsProjectRevenueLabelFrom = new JLabel("From:");
		statsProjectRevenueLabelFrom.setHorizontalAlignment(SwingConstants.RIGHT);

		JLabel statsProjectRevenueLabelTo = new JLabel("To:");
		statsProjectRevenueLabelTo.setHorizontalAlignment(SwingConstants.RIGHT);

		JLabel statsProjectRevenueLabelYear = new JLabel("Year:");
		statsProjectRevenueLabelYear.setHorizontalAlignment(SwingConstants.RIGHT);

		JLabel statsProjectRevenueLabelMonth = new JLabel("Month:");
		statsProjectRevenueLabelMonth.setHorizontalAlignment(SwingConstants.RIGHT);

		statsProjectRevenueTextFieldTo = new JTextField();
		statsProjectRevenueTextFieldTo.setEnabled(false);
		statsProjectRevenueTextFieldFrom = new JTextField();
		statsProjectRevenueTextFieldFrom.setEnabled(false);

		statsProjectRevenueRadioButtonAnually = new JRadioButton("Anually");
		statsProjectRevenueRadioButtonAnually.setOpaque(false);
		statsProjectRevenueRadioButtonAnually.addActionListener(this);

		statsProjectRevenueRadioButtonPeriod = new JRadioButton("Period");
		statsProjectRevenueRadioButtonPeriod.setOpaque(false);
		statsProjectRevenueRadioButtonPeriod.addActionListener(this);

		statsProjectRevenueRadioButtonNone = new JRadioButton("None", true);
		statsProjectRevenueRadioButtonNone.setOpaque(false);
		statsProjectRevenueRadioButtonNone.addActionListener(this);

		statsProjectRevenueButtonGroup = new ButtonGroup();
		statsProjectRevenueButtonGroup.add(statsProjectRevenueRadioButtonAnually);
		statsProjectRevenueButtonGroup.add(statsProjectRevenueRadioButtonPeriod);
		statsProjectRevenueButtonGroup.add(statsProjectRevenueRadioButtonNone);

		statsProjectRevenueComboBoxYear = new JComboBox<String>();
		statsProjectRevenueComboBoxYear.setEnabled(false);
		statsProjectRevenueComboBoxYear.addActionListener(this);

		statsProjectRevenueComboBoxMonth = new JComboBox<String>();
		statsProjectRevenueComboBoxMonth.setEnabled(false);

		statsProjectRevenuePanelPeriod.setLayout(new GridLayout(0, 5, 2, 2));
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueRadioButtonPeriod);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueLabelFrom);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueTextFieldFrom);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueLabelTo);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueTextFieldTo);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueRadioButtonAnually);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueLabelYear);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueComboBoxYear);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueLabelMonth);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueComboBoxMonth);
		statsProjectRevenuePanelPeriod.add(statsProjectRevenueRadioButtonNone);

		statsProjectRevenue.setLayout(gl_statsProjectRevenue);


		/******************************************************
		 * 
		 * statsSupplierCountry
		 * 
		 ******************************************************/

		statsSupplierCountry = new ImagePanel(image);

		String[] statsSupplierCountryTableColumns = { "", "Country", "Items" };

		statsSupplierCountryTableModel = new DefaultTableModel(null, statsSupplierCountryTableColumns);
		statsSupplierCountryTable = new JTable(statsSupplierCountryTableModel)
		{

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) 
			{
				return false;
			}

			public Class<?> getColumnClass(int column)   
			{   
				return getValueAt(0, column).getClass();   
			} 
		};

		dim = statsSupplierCountryTable.getPreferredSize(); 
		width = dim.width / statsSupplierCountryTableColumns.length; 
		double[] width4 = { 0.3, 1.5, 1, 1.1, 1.1, 1.1, 0.8, 0.8, 0.2, 0.8 };

		for (int i = 0; i < statsSupplierCountryTableColumns.length; i++)
			statsSupplierCountryTable.getColumnModel().getColumn(i).setPreferredWidth((int) (width*width4[i])); 

		TableRowSorter<DefaultTableModel> statsSupplierCountryTableSorter = new TableRowSorter<DefaultTableModel>(statsSupplierCountryTableModel);

		statsSupplierCountryTableSorter.setComparator(2, new IntComparator());

		statsSupplierCountryTable.setRowSorter(statsSupplierCountryTableSorter);
		statsSupplierCountryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JScrollPane statsSupplierCountryTableScrollPane = new JScrollPane(statsSupplierCountryTable);

		statsSupplierCountryButtonRefresh = new JButton("Refresh");
		statsSupplierCountryButtonRefresh.setOpaque(false);
		statsSupplierCountryButtonRefresh.addActionListener(this);

		GroupLayout gl_statsSupplierCountry = new GroupLayout(statsSupplierCountry);
		gl_statsSupplierCountry.setHorizontalGroup(
				gl_statsSupplierCountry.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_statsSupplierCountry.createSequentialGroup()
						.addGap(256)
						.addGroup(gl_statsSupplierCountry.createParallelGroup(Alignment.TRAILING)
								.addComponent(statsSupplierCountryButtonRefresh)
								.addComponent(statsSupplierCountryTableScrollPane, GroupLayout.PREFERRED_SIZE, 327, GroupLayout.PREFERRED_SIZE))
								.addContainerGap(207, Short.MAX_VALUE))
				);
		gl_statsSupplierCountry.setVerticalGroup(
				gl_statsSupplierCountry.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_statsSupplierCountry.createSequentialGroup()
						.addContainerGap(13, Short.MAX_VALUE)
						.addComponent(statsSupplierCountryButtonRefresh)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(statsSupplierCountryTableScrollPane, GroupLayout.PREFERRED_SIZE, 223, GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
				);
		statsSupplierCountry.setLayout(gl_statsSupplierCountry);

		/******************************************************
		 * 
		 * stuff
		 * 
		 ******************************************************/
		try
		{
			updateStatsProjectCustomerTable(statsCtrl.getCustomerProjectStatsYear(false, "", "", "", ""));
			updateStatsProjectRevenueTable(statsCtrl.getProjectRevenueStatsYear(false, "", "", "", ""));
			updateStatsSupplierNameTable(statsCtrl.getSupplierDeliveryStats("", ""));
			updateStatsSupplierNameCountryList();
			updateStatsSupplierCountry();
		}
		catch (SQLException e)
		{
			msgError(null, "Menu could not load successfully");
		}
	}

	/**
	 * ActionPerformed.. where all the magic happens.
	 * Our listener for every action which happens.
	 * 
	 * @param 	e		the event which has triggered the listener
	 * 
	 * @return 	void 
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		frameStatisticsUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

		/* PROJECT CUSTOMER */
		if(e.getSource() == statsProjectCustomerButtonSearch)
		{
			searchStatsProjectCustomer();
		}
		else if(e.getSource() == statsProjectCustomerRadioButtonAnually)
		{
			updateStatsProjectYears(statsProjectCustomerComboBoxYear, statsProjectCustomerComboBoxMonth);
			updateStatsProjectMonths(statsProjectCustomerComboBoxYear, statsProjectCustomerComboBoxMonth);

			statsProjectCustomerTextFieldFrom.setEnabled(false);
			statsProjectCustomerTextFieldTo.setEnabled(false);
		}
		else if(e.getSource() == statsProjectCustomerComboBoxYear)
		{
			updateStatsProjectMonths(statsProjectCustomerComboBoxYear, statsProjectCustomerComboBoxMonth);
		}
		else if (e.getSource() == statsProjectCustomerRadioButtonPeriod)
		{
			statsProjectCustomerTextFieldFrom.setEnabled(true);
			statsProjectCustomerTextFieldTo.setEnabled(true);
			statsProjectCustomerComboBoxYear.setEnabled(false);
			statsProjectCustomerComboBoxMonth.setEnabled(false);
		}
		else if(e.getSource() == statsProjectCustomerCheckBoxLocation)
		{
			updateStatsProjectCountry(statsProjectCustomerComboBoxCountry, statsProjectCustomerComboBoxCity, statsProjectCustomerCheckBoxLocation);
		}
		else if (e.getSource() == statsProjectCustomerComboBoxCountry)
		{
			updateStatsProjectCity(statsProjectCustomerComboBoxCountry, statsProjectCustomerComboBoxCity);
		}
		else if (e.getSource() == statsProjectCustomerRadioButtonNone)
		{
			statsProjectCustomerComboBoxYear.setEnabled(false);
			statsProjectCustomerComboBoxMonth.setEnabled(false);
			statsProjectCustomerTextFieldFrom.setEnabled(false);
			statsProjectCustomerTextFieldTo.setEnabled(false);
		}

		/* PROJECT REVENUE */
		else if(e.getSource() == statsProjectRevenueRadioButtonNone)
		{
			statsProjectRevenueComboBoxYear.setEnabled(false);
			statsProjectRevenueComboBoxMonth.setEnabled(false);
			statsProjectRevenueTextFieldFrom.setEnabled(false);
			statsProjectRevenueTextFieldTo.setEnabled(false);
		}
		else if (e.getSource() == statsProjectRevenueRadioButtonPeriod)
		{
			statsProjectRevenueComboBoxYear.setEnabled(false);
			statsProjectRevenueComboBoxMonth.setEnabled(false);
			statsProjectRevenueTextFieldFrom.setEnabled(true);
			statsProjectRevenueTextFieldTo.setEnabled(true);
		}
		else if (e.getSource() == statsProjectRevenueRadioButtonAnually)
		{
			updateStatsProjectYears(statsProjectRevenueComboBoxYear, statsProjectRevenueComboBoxMonth);
			updateStatsProjectMonths(statsProjectRevenueComboBoxYear, statsProjectRevenueComboBoxMonth);

			statsProjectRevenueTextFieldFrom.setEnabled(false);
			statsProjectRevenueTextFieldTo.setEnabled(false);
		}
		else if (e.getSource() == statsProjectRevenueComboBoxYear)
		{
			updateStatsProjectMonths(statsProjectRevenueComboBoxYear, statsProjectRevenueComboBoxMonth);		
		}
		else if (e.getSource() == statsProjectRevenueButtonSearch)
		{
			searchStatsProjectRevenue();
		}
		else if (e.getSource() == statsProjectRevenueCheckBoxLocation)
		{
			updateStatsProjectCountry(statsProjectRevenueComboBoxCountry, statsProjectRevenueComboBoxCity, statsProjectRevenueCheckBoxLocation);
		}
		else if (e.getSource() == statsProjectRevenueComboBoxCountry)
		{
			updateStatsProjectCity(statsProjectRevenueComboBoxCountry, statsProjectRevenueComboBoxCity);
		}

		/* SUPPLIER NAME */
		else if (e.getSource() == statsSupplierNameComboBoxCountry)
		{
			try
			{
				String locationCountry = statsSupplierNameComboBoxCountry.getSelectedIndex() > 0 ? statsSupplierNameComboBoxCountry.getSelectedItem().toString() : "";

				updateStatsSupplierComboBoxCity();
				updateStatsSupplierNameTable(statsCtrl.getSupplierDeliveryStats(locationCountry, ""));
			}
			catch (SQLException e1)
			{
				msgError(statsSupplierName, "Could not load countries and cities");
			}
		}
		else if (e.getSource() == statsSupplierNameComboBoxCity)
		{
			try
			{
				String locationCountry = statsSupplierNameComboBoxCountry.getSelectedIndex() > 0 ? statsSupplierNameComboBoxCountry.getSelectedItem().toString() : "";
				String locationCity = statsSupplierNameComboBoxCity.getSelectedIndex() > 0 ? statsSupplierNameComboBoxCity.getSelectedItem().toString() : "";

				updateStatsSupplierNameTable(statsCtrl.getSupplierDeliveryStats(locationCountry, locationCity));
			}
			catch (SQLException e1)
			{
				msgError(statsSupplierName, "Could not load countries and cities");
			}
		}

		/* SUPPLIER COUNTRY */
		else if (e.getSource() == statsSupplierCountryButtonRefresh)
		{
			updateStatsSupplierCountry();
		}

		else if(e.getSource() == btnBack)
		{
			back.goBack(back.getNumber());
		}
		else if (e.getSource() == btnPrint)
		{
			int tab = tabbedPane.getSelectedIndex();

			switch(tab)
			{
				case 1: printProjectRevenuePDF(); break;
				case 2: printSupplierNamePDF(); break;
				case 3: printSupplierCountryPDF(); break;
				default: printProjectCustomerPDF(); break;
			}
		}
		frameStatisticsUI.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	private void printProjectCustomerPDF()
	{
		// 		String[] statsProjectCustomerTableColumns = { "#", "Customer", "City", "Country", "", "Projects", "Project Price", "Item Price", "Revenue" };
		if (statsProjectCustomerTable.getRowCount() > 0)
		{
			ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

			String[] columns = { "#", "Customer", "City", "Country", "Projects", "Project Price", "Item Price", "Revenue" };

			ArrayList<String> pcID = new ArrayList<String>();
			ArrayList<String> pcCustomer = new ArrayList<String>();
			ArrayList<String> pcCity = new ArrayList<String>();
			ArrayList<String> pcCountry = new ArrayList<String>();
			ArrayList<String> pcProjects = new ArrayList<String>();
			ArrayList<String> pcProjectPrice = new ArrayList<String>();
			ArrayList<String> pcItemPrice = new ArrayList<String>();
			ArrayList<String> pcRevenue = new ArrayList<String>();

			list.add(pcID);
			list.add(pcCustomer);
			list.add(pcCity);
			list.add(pcCountry);
			list.add(pcProjects);
			list.add(pcProjectPrice);
			list.add(pcItemPrice);
			list.add(pcRevenue);

			for(int i = 0; i < statsProjectCustomerTable.getRowCount(); i++)
			{
				list.get(0).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 0)));
				list.get(1).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 1)));
				list.get(2).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 2)));
				list.get(3).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 3)));
				list.get(4).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 5)));
				list.get(5).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 6)));
				list.get(6).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 7)));
				list.get(7).add(String.valueOf(statsProjectCustomerTable.getValueAt(i, 8)));
			}

			PdfFilePrint pdfPrint = new PdfFilePrint();
			boolean successPDF = pdfPrint.PDFPrint("Statistics-Project-Customer", "Project Customer Statistics", "stats", "Nikola", columns, list);

			if(successPDF)
				msgSuccess(null, "PDF was created successfully");
			else
				msgError(null, "There was an error creating the PDF");
		}
		else
		{
			msgError(null, "Nothing to print");
		}
	}

	private void printProjectRevenuePDF()
	{
		//String[] statsProjectRevenueTableColumns = { "#", "Project Name", "Price Item", "Price Project", "Revenue", "Sub Contractor Money", "Customer", "City", "Country", "", "Date" };
		if (statsProjectRevenueTable.getRowCount() > 0)
		{
			ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

			String[] columns = { "#", "Project Name", "Price Item", "Price Project", "Revenue", "Sub Contractor Money", "Customer", "City", "Country", "Date" };
			
			ArrayList<String> prID = new ArrayList<String>();
			ArrayList<String> prName = new ArrayList<String>();
			ArrayList<String> prPriceItem = new ArrayList<String>();
			ArrayList<String> prPriceProject = new ArrayList<String>();
			ArrayList<String> prRevenue = new ArrayList<String>();
			ArrayList<String> prSubContractorMoney = new ArrayList<String>();
			ArrayList<String> prCustomer = new ArrayList<String>();
			ArrayList<String> prCity = new ArrayList<String>();
			ArrayList<String> prCountry = new ArrayList<String>();
			ArrayList<String> prDate = new ArrayList<String>();

			list.add(prID);
			list.add(prName);
			list.add(prPriceItem);
			list.add(prPriceProject);
			list.add(prRevenue);
			list.add(prSubContractorMoney);
			list.add(prCustomer);
			list.add(prCity);
			list.add(prCountry);
			list.add(prDate);

			for(int i = 0; i < statsProjectRevenueTable.getRowCount(); i++)
			{
				list.get(0).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 0)));
				list.get(1).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 1)));
				list.get(2).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 2)));
				list.get(3).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 3)));
				list.get(4).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 4)));
				list.get(5).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 5)));
				list.get(6).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 6)));
				list.get(7).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 7)));
				list.get(8).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 8)));
				list.get(9).add(String.valueOf(statsProjectRevenueTable.getValueAt(i, 10)));

			}

			PdfFilePrint pdfPrint = new PdfFilePrint();
			boolean successPDF = pdfPrint.PDFPrint("Statistics-Project-Revenue", "Project Revenue Statistics", "stats", "Nikola", columns, list);

			if(successPDF)
				msgSuccess(null, "PDF was created successfully");
			else
				msgError(null, "There was an error creating the PDF");
		}
		else
		{
			msgError(null, "Nothing to print");
		}
	}

	private void printSupplierNamePDF()
	{
		//String[] statsSupplierNameTableColumns = { "#", "Supplier Name", "Number of Items", "Country", "City", "", "Active" };

		if (statsSupplierNameTable.getRowCount() > 0)
		{
			ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

			String columns [] = { "#", "Name", "Number of Items", "Country", "City", "Active" };

			ArrayList<String> snID = new ArrayList<String>();
			ArrayList<String> snName = new ArrayList<String>();
			ArrayList<String> snItems = new ArrayList<String>();
			ArrayList<String> snCountry = new ArrayList<String>();
			ArrayList<String> snCity = new ArrayList<String>();
			ArrayList<String> snActive = new ArrayList<String>();

			list.add(snID);
			list.add(snName);
			list.add(snItems);
			list.add(snCountry);
			list.add(snCity);
			list.add(snActive);

			for(int i = 0; i < statsSupplierNameTable.getRowCount(); i++)
			{
				list.get(0).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 0)));
				list.get(1).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 1)));
				list.get(2).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 2)));
				list.get(3).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 3)));
				list.get(4).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 4)));
				list.get(5).add(String.valueOf(statsSupplierNameTable.getValueAt(i, 6)).contains("yes") ? "Yes" : "No");
			}

			PdfFilePrint pdfPrint = new PdfFilePrint();
			boolean successPDF = pdfPrint.PDFPrint("Statistics-Supplier-Name", "Supplier Name Statistics", "stats", "Nikola", columns, list);

			if(successPDF)
				msgSuccess(null, "PDF was created successfully");
			else
				msgError(null, "There was an error creating the PDF");
		}
		else
		{
			msgError(null, "Nothing to print");
		}
	}

	private void printSupplierCountryPDF()
	{
		if (statsSupplierCountryTable.getRowCount() > 0)
		{
			ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

			String columns [] = {  "Country", "Items" };

			ArrayList<String> scCountry = new ArrayList<String>();
			ArrayList<String> scItems = new ArrayList<String>();

			list.add(scCountry);
			list.add(scItems);

			for(int i = 0; i < statsSupplierCountryTable.getRowCount(); i++)
			{
				list.get(0).add(String.valueOf(statsSupplierCountryTable.getValueAt(i, 1)));
				list.get(1).add(String.valueOf(statsSupplierCountryTable.getValueAt(i, 2)));
			}

			PdfFilePrint pdfPrint = new PdfFilePrint();
			boolean successPDF = pdfPrint.PDFPrint("Statistics-Supplier-Country", "Supplier Country Statistics", "stats", "Nikola", columns, list);

			if(successPDF)
				msgSuccess(null, "PDF was created successfully");
			else
				msgError(null, "There was an error creating the PDF");
		}
		else
		{
			msgError(null, "Nothing to print");
		}
	}

	private void updateStatsSupplierCountry()
	{
		tableClearList(statsSupplierCountryTable);

		LinkedList<Statistics<Country>> list = new LinkedList<Statistics<Country>>();

		try
		{
			list = statsCtrl.getCountrySupplierStats();
		}
		catch(SQLException e)
		{
			msgError(statsSupplierCountry, "Could not load countries");
		}

		if(list.size() > 0)
		{
			for(Statistics<Country> s : list)
			{
				statsSupplierCountryTableModel.addRow(
						new Object[] 
								{
								new ImageIcon("img/flags/" + s.getElement().getTld() + ".png"),
								s.getElement().getName(),
								s.getCount()
								});
			}
		}
	}

	private void updateStatsSupplierNameCountryList()
	{
		try
		{
			ArrayList<String> list = statsCtrl.getSupplierDeliveryCountries();
			populateCombobox(true, statsSupplierNameComboBoxCountry, list);
		}
		catch (SQLException e)
		{
			msgError(statsSupplierName, "Could not load countries");
		}
	}

	private void updateStatsSupplierNameTable(LinkedList<Statistics<Outsider>> list)
	{
		tableClearList(statsSupplierNameTable);

		if(list.size() > 0)
		{
			for(Statistics<Outsider> s : list)
			{
				statsSupplierNameTableModel.addRow(
						new Object[] 
								{
								s.getElement().getOutsiderID(),
								s.getElement().getName(),
								s.getCount(),
								s.getElement().getCity().getCountry().getName(),
								s.getElement().getCity().getName(),
								new ImageIcon("img/flags/" + s.getElement().getCity().getCountry().getTld() + ".png"),
								new ImageIcon("img/tick_" + (s.getElement().isActive() ? "yes" : "no") + ".png")
								});
			}
		}
	}

	private void updateStatsProjectRevenueTable(LinkedList<Statistics<Project>> list)
	{
		tableClearList(statsProjectRevenueTable);

		if (list.size() > 0)
		{
			for(Statistics<Project> s : list)
			{
				statsProjectRevenueTableModel.addRow(
						new Object[] 
								{
								s.getElement().getServiceID(),
								s.getElement().getName(),
								s.getPriceItem(),
								s.getPriceProject(),
								s.getRevenue(),
								s.getProjectSubcontractorMoney(),
								s.getElement().getCustomer().getUsername(),
								s.getElement().getCustomer().getCity().getName(),
								s.getElement().getCustomer().getCity().getCountry().getName(),
								new ImageIcon("img/flags/" + s.getElement().getCustomer().getCity().getCountry().getTld() + ".png"),
								s.getElement().getDeadline()
								});
			}
		}

		statsProjectRevenueLabelProjects.setText(String.valueOf(list.size()));
	}

	private void updateStatsSupplierComboBoxCity() throws SQLException
	{
		statsSupplierNameComboBoxCity.setEnabled(statsSupplierNameComboBoxCountry.getSelectedIndex() > 0);

		if (statsSupplierNameComboBoxCountry.getSelectedIndex() > 0)
			populateCombobox(true, statsSupplierNameComboBoxCity, statsCtrl.getSupplierDeliveryCities(statsSupplierNameComboBoxCountry.getSelectedItem().toString()));
	}

	private void searchStatsProjectRevenue()
	{
		String locationCountry = "";
		String locationCity = "";

		if(statsProjectRevenueCheckBoxLocation.isSelected())
		{
			locationCountry = statsProjectRevenueComboBoxCountry.getSelectedItem().toString();
			locationCity = statsProjectRevenueComboBoxCity.getSelectedIndex() > 0 ? statsProjectRevenueComboBoxCity.getSelectedItem().toString() : "";
		}

		LinkedList<Statistics<Project>> list = new LinkedList<Statistics<Project>>();

		try
		{
			if (statsProjectRevenueRadioButtonPeriod.isSelected())
			{
				String dateStart = statsProjectRevenueTextFieldFrom.getText();
				String dateEnd = statsProjectRevenueTextFieldTo.getText();

				if (isLegalDate(dateStart) && isLegalDate(dateEnd))
				{
					list = statsCtrl.getProjectRevenueStatsPeriod(true, dateStart, dateEnd, locationCountry, locationCity);
				}
				else
				{
					throw new IllegalArgumentException("You have put an invalid date.\nPlease use the YYYY-MM-DD format.\nExample: 2012-12-31");
				}
			}
			else if (statsProjectRevenueRadioButtonAnually.isSelected())
			{
				String dateYear = statsProjectRevenueComboBoxYear.getSelectedItem().toString();
				String dateMonth = statsProjectRevenueComboBoxMonth.getSelectedIndex() > 0 ? statsProjectRevenueComboBoxMonth.getSelectedItem().toString() : "";

				list = statsCtrl.getProjectRevenueStatsYear(true, dateYear, dateMonth, locationCountry, locationCity);
			}
			else
			{
				list = statsCtrl.getProjectRevenueStatsYear(false, "", "", locationCountry, locationCity);
			}

			updateStatsProjectRevenueTable(list);
		}
		catch(SQLException ex)
		{
			msgError(statsProjectCustomer, "There was an error retrieveing the list");
		}
		catch(IllegalArgumentException il)
		{
			msgError(statsProjectCustomer, il.getMessage());
		}
	}

	private void searchStatsProjectCustomer()
	{
		String locationCountry = "";
		String locationCity = "";

		if(statsProjectCustomerCheckBoxLocation.isSelected())
		{
			locationCountry = statsProjectCustomerComboBoxCountry.getSelectedItem().toString();
			locationCity = statsProjectCustomerComboBoxCity.getSelectedIndex() > 0 ? statsProjectCustomerComboBoxCity.getSelectedItem().toString() : "";
		}

		LinkedList<Statistics<Customer>> list = new LinkedList<Statistics<Customer>>();

		try
		{
			if (statsProjectCustomerRadioButtonPeriod.isSelected())
			{
				String dateStart = statsProjectCustomerTextFieldFrom.getText();
				String dateEnd = statsProjectCustomerTextFieldTo.getText();

				if (isLegalDate(dateStart) && isLegalDate(dateEnd))
				{
					list = statsCtrl.getCustomerProjectStatsPeriod(true, dateStart, dateEnd, locationCountry, locationCity);
				}
				else
				{
					throw new IllegalArgumentException("You have put an invalid date.\nPlease use the YYYY-MM-DD format.\nExample: 2012-12-31");
				}
			}
			else if (statsProjectCustomerRadioButtonAnually.isSelected())
			{
				String dateYear = statsProjectCustomerComboBoxYear.getSelectedItem().toString();
				String dateMonth = statsProjectCustomerComboBoxMonth.getSelectedIndex() > 0 ? statsProjectCustomerComboBoxMonth.getSelectedItem().toString() : "";

				list = statsCtrl.getCustomerProjectStatsYear(true, dateYear, dateMonth, locationCountry, locationCity);
			}
			else
			{
				list = statsCtrl.getCustomerProjectStatsYear(false, "", "", locationCountry, locationCity);
			}

			updateStatsProjectCustomerTable(list);
		}
		catch(SQLException ex)
		{
			msgError(statsProjectCustomer, "There was an error retrieveing the list");
		}
		catch(IllegalArgumentException il)
		{
			msgError(statsProjectCustomer, il.getMessage());
		}
	}

	private void updateStatsProjectCity(JComboBox<String> comboboxCountry, JComboBox<String> comboboxCity)
	{
		if (comboboxCountry.isEnabled())
		{
			try
			{
				populateCombobox(true, comboboxCity, statsCtrl.getProjectCities(comboboxCountry.getSelectedItem().toString()));
			}
			catch(SQLException e)
			{
				msgError(null, "Could not load cities");
			}
		}
	}

	private void updateStatsProjectCountry(JComboBox<String> comboboxCountry, JComboBox<String> comboboxCity, JCheckBox checkboxLocation)
	{
		comboboxCity.setEnabled(checkboxLocation.isSelected());
		comboboxCountry.setEnabled(checkboxLocation.isSelected());

		if(checkboxLocation.isSelected())
		{
			try
			{
				populateCombobox(false, comboboxCountry, statsCtrl.getProjectCountries());
			}
			catch(SQLException e)
			{
				msgError(null, "Could not load countries");
			}
		}
		else
		{
			comboboxCity.removeAllItems();
			comboboxCountry.removeAllItems();
		}
	}

	private void updateStatsProjectMonths(JComboBox<String> comboboxYear, JComboBox<String> comboboxMonth)
	{
		if (comboboxYear.isEnabled())
		{
			try
			{
				populateCombobox(true, comboboxMonth, statsCtrl.getProjectAllMonths(comboboxYear.getSelectedItem().toString()));
			}
			catch (SQLException e)
			{
				msgError(null, "Could not load months");
			}
		}
	}

	private void updateStatsProjectYears(JComboBox<String> comboboxYear, JComboBox<String> comboboxMonth)
	{
		try
		{
			populateCombobox(false, comboboxYear, statsCtrl.getProjectAllYears());

			comboboxYear.setEnabled(true);
			comboboxMonth.setEnabled(true);
		}
		catch (SQLException e)
		{
			msgError(null, "Could not load years");
		}
	}

	private void updateStatsProjectCustomerTable(LinkedList<Statistics<Customer>> list) throws SQLException
	{
		tableClearList(statsProjectCustomerTable);

		if(list.size() > 0)
		{
			for(Statistics<Customer> s : list)
			{
				statsProjectCustomerTableModel.addRow(
						new Object[] 
								{
								s.getElement().getPersonID(),
								s.getElement().getUsername(),
								s.getElement().getCity().getName(),
								s.getElement().getCity().getCountry().getName(),
								new ImageIcon("img/flags/" + s.getElement().getCity().getCountry().getTld() + ".png"),
								s.getCount(),
								s.getPriceProject(),
								s.getPriceItem(),
								s.getRevenue()
								});
			}
		}

		statsProjectCustomerLabelResults.setText(String.valueOf(list.size()));
	}

	private void populateCombobox(boolean notEmpty, JComboBox<String> combobox, ArrayList<String> list)
	{
		combobox.removeAllItems();

		if (notEmpty)
			combobox.addItem("All");

		for(String s : list)
		{
			combobox.addItem(s);
		}
	}

	private	boolean isLegalDate(String s)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);

		return sdf.parse(s, new ParsePosition(0)) != null;
	}

	/**
	 * Removing all rows from a table
	 *
	 * @param 	table	table to clear
	 * 
	 * @return	void
	 */
	private void tableClearList(JTable table)
	{
		DefaultTableModel dm = (DefaultTableModel) table.getModel();

		while(dm.getRowCount() > 0)
			dm.removeRow(0);
	}

	/**
	 * Shows an error message pop-up
	 * 
	 * @param	panel 		on which panel should it show the message
	 * @param	error		the error which is printed on the pop-up window
	 * 
	 * @return	void
	 */
	private void msgError(JPanel panel, String error)
	{
		JOptionPane.showConfirmDialog(panel, error, "Error", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);
	}

	private void msgSuccess(JPanel panel, String success)
	{
		JOptionPane.showMessageDialog(panel, success, "Success", 1);
	}

	public static StatisticsUI getInstance()
	{
		if(instance == null)
			instance = new StatisticsUI();

		return instance;			
	}

	public static void main(String[] args)
	{
		try 
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

			StatisticsUI statsMenu = new StatisticsUI();
			statsMenu.display();
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Statistics Menu could not load");
		}
	}
}